package com.se.clickableText;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;
import com.se.contextMenu.MessageContextMenu;
import com.se.utils.ServiceUtils;
import javafx.scene.text.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//非常规词汇检测，主要逻辑是AI写的
public class UnusualWordDetector {

    // 匹配纯字母、数字组合（如YYDS）
    private static final Pattern UNUSUAL_PATTERN = Pattern.compile("[a-zA-Z]+");
    private static final Pattern AT_PATTERN = Pattern.compile("\\s(@\\S+)\\s");

    public static List<Text> createUnusualRichText(String text) {
        List<Text> texts = new ArrayList<>();

        Matcher matcher = AT_PATTERN.matcher(text);

        int lastIndex = 0;

        while(matcher.find()) {
            //非AT消息
            if(matcher.start() > lastIndex) {
                if(!ServiceUtils.AUTO_DETECT_TEXT) {
                    texts.add(new Text(text.substring(lastIndex, matcher.start())));
                }
                else {
                    //分词
                    List<Term> terms = HanLP.segment(text.substring(lastIndex, matcher.start()));

                    for(Term term : terms) {
                        String word = term.word;

                        if (isUnusualPattern(word)) {
                            ClickableText clickableText = new ClickableText(word,
                                    () -> MessageContextMenu.askAI(word));
                            texts.add(clickableText);
                        }
                        else {
                            texts.add(new Text(word));
                        }
                    }
                }
            }

            //AT消息
            Text atText;
            if(matcher.group(1).contains(ServiceUtils.getSpeaker().getUserName())) {
                atText = ClickableText.createAtText(matcher.group(1), true);
            }
            else {
                atText = ClickableText.createAtText(matcher.group(1), false);
            }
            texts.add(atText);
            lastIndex = matcher.end();
        }

        if(lastIndex < text.length()) {
            if(!ServiceUtils.AUTO_DETECT_TEXT) {
                texts.add(new Text(text.substring(lastIndex)));
            }
            else {
                //分词
                List<Term> terms = HanLP.segment(text.substring(lastIndex));

                for(Term term : terms) {
                    String word = term.word;

                    if (isUnusualPattern(word)) {
                        ClickableText clickableText = new ClickableText(word,
                                () -> MessageContextMenu.askAI(word));
                        texts.add(clickableText);
                    }
                    else {
                        texts.add(new Text(word));
                    }
                }
            }
        }

        return texts;
    }

    private static boolean isUnusualPattern(String word) {
        // 判断是否为纯字母/数字组合（可根据需求调整规则）
        return UNUSUAL_PATTERN.matcher(word).matches();
    }

    public static void main(String[] args) {
        String text = "你是YYDS";
//        List<String> unusualWords = detectUnusualWords(text);
//        System.out.println("非常规词汇: " + unusualWords); // 输出: [YYDS]
    }
}
